﻿using System;
using System.Collections.Generic;

namespace BuzzX8.Xmpp.StreamElements.Factories
{
    class DefaultFactoryProvider : IFactoryProvider
    {
        private IDictionary<string, StreamElementFactory> factories;

        public DefaultFactoryProvider()
        {
            factories = new Dictionary<string, StreamElementFactory>();
            AddDefaultFactories();
        }

        private void AddDefaultFactories()
        {
            factories.Add("stream:stream", new StreamHeaderFactory());
            factories.Add("presence", new PresenceStanzaFactory());
            factories.Add("message", new MessageStanzaFactory());
            factories.Add("iq", new IqFactory());
        }

        public StreamElementFactory GetFactory(string elementName)
        {
            if (factories.ContainsKey(elementName)) return factories[elementName];
            throw new ArgumentException("Can't find factory");
        }

        public T GetFactory<T>() where T : StreamElementFactory
        {
            foreach (StreamElementFactory factory in factories.Values) if (factory is T) return (T)factory;
            throw new ArgumentException("Can't find required factory");
        }
    }
}
